<?php

class Danhsachcongty_DanhsachcongtyController extends Zend_Controller_Action {

	public function init() {
		$this->params = $this->getRequest()->getparams();
		System_Template::using("blank", "Admin");
		$this->model = new Danhsachcongty_Model_Danhsachcongty();
		parent::init();
	}

	function indexAction() {
		$this->view->maintitle = "Danh sách công ty";
		$model = new Danhsachcongty_Model_Danhsachcongty();
		$name = $this->_request->getParam("name");
		$phamvi = $this->_request->getParam("phamvi");
		$this->view->name = $name;
		$this->view->phamvi = $phamvi;
		$this->view->website = $website;
		$parameter = array('name' => $name, 'website' => $website,
							'phamvi' => $phamvi);
		$count = $model->count($parameter);
		$paging = common::pagingAuto($count);
		$data = $model->SelectAll($parameter, $paging[0], $paging[1]);
		$this->view->data = $data;
		common::showButton('delete');
		common::showButton('add');
	}
	public function inputAction() {
		common::showButton('save');
		common::showButton('back');
		$this->qldscty = new Danhsachcongty_Model_Danhsachcongty();
		$id = $this->params['id'];
		$this->view->flag = 0;
		if ($id == 0) {
			$this->view->maintitle = "Thêm công ty vào danh sách";
		} else {
			$this->view->maintitle = "Cập nhật thông tin công ty";
			$data = $this->qldscty->FindById($id);
			$this->view->data = $data;
			$this->view->create_date = $this->model
					->convertDate1($data['creation_date']);
			$this->view->id = $id;
			$this->view->flag = 1;
		}
	}
	public function saveAction() {
		$uploader = new System_Upload();
		$uploadInfo = $uploader->upload();
        
        $this->params['creation_date'] = $this->model->convertDate($this->params['creation_date']);
        $this->params['image'] = $uploadInfo['path'].$uploadInfo[0]['name'];

		$r = $this->model->CheckExitsName($this->params['name']);
		$this->model->AddNewCompany($this->params);
		$this->_redirect('/danhsachcongty/danhsachcongty/index');
	}
	public function deleteAction() {
		$params = $this->getRequest()->getParams();
		$db = Zend_Db_Table::getDefaultAdapter();
		$ids = $params["DEL"];
		for ($i = 0; $i < count($ids); $i++) {
			$this->model->DeleteCompany($ids[$i]);
		}
		$this->_redirect("/danhsachcongty/danhsachcongty/index");
	}
}

